#include "MediaApi.h"
#include "MediaLib.h"
#include "MediaChannel.h"

#include "HttpCmdProcess.h"
#include "CurtainServer.h"
#include "StreamFileServer.h"
#include "WifiServer.h"
#include "NetWorkServer.h"
#include "ResetServer.h"
#include "RtspClientStream.h"
#include "SystemServer.h"
#include "DiskServer.h"
#include "CurtainActManager.h"
#include "StreamerCurtainPlay.h"
#include "Gui/GuiDisplay.h"
#include "BgmCurtainPlay.h"
#include "BgmListManager.h"
#include "FtpServer.h"
#include "ConfigApi.h"


using namespace ConfigApi;

namespace Media{
    class DeviceMediaApp:public IMediaInterface{
    public:
        DeviceMediaApp():IMediaInterface(){
            CurtainServer::instance();
            StreamFileServer::instance();
            NetWorkServer::instance();
            ResetServer::instance();
            WifiServer::instance();
            SystemServer::instance();
            DiskServer::instance();
            FtpServer::instance();
        }
        bool Start() override{
            MediaLibRun();
            GuiDisplay::Instance();
            return true;
        }
    	bool Stop() override{
            return true;
        }
    	/*
        功能描述: 获取媒体通道信息
    	返回值：返回媒体通道信息对象智能指针
    	*/
    	MediaChannel::Ptr GetMediaChannel(int video, int audio) override{
            return std::make_shared<CDeviceMediaChannel>(video, audio);
        }

        /*
    	功能描述：设置拉流创建器
    	返回值：成功/true, 失败/false
        */
    	bool SetStreamPlayCreater(StreamPlayCreater::Ptr creater) override{
            SetMediaStreamCreater(creater);
            return true;
        }

        /*
    	功能描述：停止读取U盘中的文件
    	返回值：成功/true, 失败/false
        */
    	bool StopReadUsbCard(){
            int num = 0;
            if(!CurtainActManager::instance()->getCurPlayNum(&num)){
                errorf("getCurPlayNum faild\n");
            }
            if(num == StreamerNum){
                return StreamerCurtainPlay::instance()->stopUsbcardStreamer();
            }


            Json::Value bgmTable;
            IConfigInterface::GetInstance()->Get("BgmInfo", bgmTable);
            if(bgmTable.isMember("BgmPath") && bgmTable["BgmPath"].isString())
            {
                std::string path = bgmTable["BgmPath"].asString();
            
                if(path.size() > 0 && path.find(AIR_UDISK_PATH) != std::string::npos)
                {
                    Media::BgmCurtainPlay::instance()->stopUsbcardBgmCurtain();
                    //清除bgm的路径信息
                    bgmTable["BgmPath"] = "";
                    IConfigInterface::GetInstance()->Set("BgmInfo", bgmTable, ConfigOptions::ConfigOptionPart);
                }		
            }
            return true;
        }

        //显示SD卡或U盘格式不支持的弹框
		void DisplayWarnSD(){
            GuiDisplay::Instance()->DisplayWarnSD();
        }
		//关闭SD卡或U盘格式不支持的弹框
		void CloseDisplayWarnSD(){
            GuiDisplay::Instance()->CloseDisplayWarnSD();
        }
    };

    std::shared_ptr<IMediaInterface> IMediaInterface::GetInstance(){
        static std::shared_ptr<IMediaInterface> interface = std::make_shared<DeviceMediaApp>();
        return interface;
    }
}
